package com.hwl.cms.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwl.cms.pojo.AdType;
import com.hwl.cms.service.AdTypeService;
import com.hwl.commonutils.result.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 推荐位 前端控制器
 * </p>
 *
 * @author long
 * @since 2021-10-06
 */
@Api(tags = "广告推荐位管理")
@RestController
@RequestMapping("/admin/cms/adType")
public class AdTypeController {

    @Resource
    private AdTypeService adTypeService;

    @ApiOperation("获取所有推荐位类别列表")
    @GetMapping("list")
    public RespBean listAll() {
        return RespBean.ok().data(adTypeService.list(null));
    }

    @ApiOperation("推荐位类别分页列表")
    @GetMapping("list/{page}/{limit}")
    public RespBean listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                             @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit) {
        IPage<AdType> iPage = adTypeService.page(new Page<>(page, limit), null);
        return RespBean.ok().data(iPage);
    }

    @ApiOperation("根据ID删除推荐位类别")
    @DeleteMapping("remove/{id}")
    public RespBean removeById(@PathVariable String id) {
        if (adTypeService.removeById(id)) {
            return RespBean.ok().message("删除推荐位成功！");
        }
        return RespBean.error().message("删除推荐位失败！");
    }

    @ApiOperation("新增推荐位类别")
    @PostMapping("save")
    public RespBean save(@RequestBody AdType adType) {
        if (adTypeService.save(adType)) {
            return RespBean.ok().message("新增推荐位成功！");
        }
        return RespBean.error().message("新增推荐位失败！");
    }

    @ApiOperation("更新推荐位类别")
    @PutMapping("update")
    public RespBean updateById(@RequestBody AdType adType) {
        if (adTypeService.updateById(adType)) {
            return RespBean.ok().message("更新推荐位成功！");
        }
        return RespBean.error().message("更新推荐位失败！");
    }

    @ApiOperation("根据id获取推荐位类别信息")
    @GetMapping("get/{id}")
    public RespBean getById(@PathVariable String id) {
        return RespBean.ok().data(adTypeService.getById(id));
    }


}

